<?php

require_once 'Base/UserPeer.php';

/**
 * Generated Model class.
 */
class UserPeer extends Base_UserPeer
{
	/**
	 * Сохраняет данные о пользователе
	 * @param array $info
	 * @param array $groups
	 */
	public function saveUserInfo($info, $groups){
		$name = $info['first_name'] . ' ' . $info['last_name'];
		$groups = Zend_Json::encode($groups);
		
		$select = $this->select()->where('vk_id = ?', $info['uid']);
		$row = $this->fetchRow($select);
		if($row){
			// update
			$data = array(
				'groups' => $groups,
                                'access_token' => Zend_Registry::get('VK')->getAccessToken(),
				'modified' => date('Y-m-d H:i:s')
			);
			$this->update($data, 'vk_id = ' . $info['uid']);
			return true;
		}else{
			// create
			$data = array(
				'id' => '',
				'vk_id' => $info['uid'],
				'name' => $name,
				'groups' => $groups,
				'sex' => $info['sex'],
				'access_token' => Zend_Registry::get('VK')->getAccessToken(),
				'created' => date('Y-m-d H:i:s'),
				'modified' => date('Y-m-d H:i:s')
			);
			$this->insert($data);
			ActivityPeer::getInstance()->addUser($info['uid'], $name);
			return true;
		}
		return false;
	}
        
	/**
	 * Возвращает информацию о пользователе
	 * @param int $id
	 */
	public function getUserInfo($id){
		$select = $this->select()->where('vk_id = ?', $id);
		$row = $this->fetchRow($select);
		if($row){
			return $row->toArray();
		}
		return null;
	}
}

